package org.jiuling.qr.google;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.jiuling.local.tools.enums.CharsetEnum;
import org.jiuling.local.tools.verify.Assertions;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;


/**
 * 二维码
 * @author 紫灵
 * @date 2022年10月25日 13时33分25秒
 */
public class QRCode {


    /** 字符集 */
    private final CharsetEnum charset;
    /** 格式 */
    private final String format;
    /** 二维码高度 */
    private final int qrHeight;
    /** 二维码长度 */
    private final int qrWidth;
    /** logo宽度 */
    private final int logoWidth;
    /** logo高度 */
    private final int logoHeight;
    /** 压缩LOGO */
    private final boolean compressLogo;
    public static final int FRONT_COLOR=0x000000;
    public static final int BACKGROUND_COLOR=0xFFFFFF;

    public QRCode(CharsetEnum charset, String format, int qrHeight, int qrWidth, Integer logoWidth, Integer logoHeight, boolean compressLogo) {
        this.charset = charset;
        this.format = format;
        this.qrHeight = qrHeight;
        this.qrWidth = qrWidth;
        this.logoWidth = logoWidth;
        this.logoHeight = logoHeight;
        this.compressLogo = compressLogo;
    }

    public QRCode(CharsetEnum charset, String format, int qrHeight, int qrWidth) {
        this.charset = charset;
        this.format = format;
        this.qrHeight = qrHeight;
        this.qrWidth = qrWidth;
        this.compressLogo = false;
        this.logoWidth = 0;
        this.logoHeight = 0;
    }

    /**
     * 创建二维码
     * @param content 二维码内容
     * @return
     */
    public BufferedImage createImage(String content){
        Map<EncodeHintType,Object> hints = new HashMap<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, charset.getCapital());
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = null;
        try {
            // 修改二维码底部高度
            bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,qrWidth , qrHeight,
                    hints);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return image;
    }

    /**
     * 对已经生成好的二维码设置logo
     * @param source 二维码
     * @param logo logo图片
     */
    public void insertLogoImage(BufferedImage source,Image logo){
        Graphics2D graph = source.createGraphics();
        int qrWidth = source.getWidth();
        int qrHeight = source.getHeight();
        int x = (qrWidth - logoWidth) / 2;
        int y = (qrHeight - logoHeight) / 2;
        graph.drawImage(logo, x, y, logoWidth, logoHeight, null);
        Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 6, 6);
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);
        graph.dispose();
    }

    public void encode(String content,OutputStream outputStream) throws IOException, WriterException {
        Assertions.stringIsNotBlank("生成内容不能为空！",content);
        BufferedImage bufferedImage = createImage(content);
        ImageIO.write(bufferedImage,format,outputStream);
    }

    public static void main(String[] args) throws IOException, WriterException {
        QRCode qrCode = new QRCode(CharsetEnum.UTF_8,"jpg", 500, 500);
        File file = new File("/home/lym/Pictures/qrcodeImage.jpg");
        OutputStream out = new FileOutputStream(file);
        qrCode.encode("https://api.langekuaiyun.com?orderNo=LG240228185544674",out);
    }

}
